﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SimCMS.Manager;
using SimCMS.Model;

namespace SimCMS.Controllers
{
    [HandleError]
    public class AdminController : Controller
    {
        public ISimadminManager SimadminManager { get; set; }
        public ISimadminroleManager SimadminroleManager { get; set; }
        public ISimadminmenuManager SimadminmenuManager { get; set; }

        public ActionResult Login()
        {
            //var ListPanel = new List<Simadminpanel>();
            ////var mm = new Simadminpanel
            ////{
            ////    MenuDatetime = DateTime.Now,
            ////    Name = "test"
            ////};
            ////ListPanel.Add(mm);
            //var admin = new Simadmin
            //{
            //    UserName = "abcd",
            //    Password = "453",
            //    Simadminrole = SimadminroleManager.Get(1)
            //    //Simadminpanel = ListPanel
            //};
            //SimadminManager.Save(admin);
            return View();
        }

        [HttpPost]
        public ActionResult Login(Simadmin Model)
        {

            if (SimadminManager.Login(Model))
            {
                return RedirectToAction("Index", "Home");
            }
            else
            {
                return Content("Error");
            }
        }

        public ActionResult AdminList(int Index)
        {
            if (Request["pageNum"] != null)
            {
                Index = int.Parse(Request["pageNum"]);
            }
            var Modelist = SimadminManager.LoadAll();
            SimCMS.Common.PageUnit<Simadmin> UnitPage = new Common.PageUnit<Simadmin>(Modelist.ToList(), SimCMS.Common.CommonHelper.PageSize, Index);
            ViewBag.Index = Index;
            ViewBag.PageSize = UnitPage.PageSize;
            ViewBag.Total = Modelist.Count;
            return View(UnitPage.ToList());
        }
        [HttpPost]
        public ActionResult AdminDelete(int Id)
        {
            SimadminManager.Delete(Id);

            return Json(SimCMS.Common.MessageHelper.SendMessge("成功删除", Common.MessageType.Info, false));
        }

        public ActionResult AdminContent(int? Id)
        {
            GetRoleList();
            if (Id != null && Id != 0)
            {
                var AdminModel = SimadminManager.Get(Id);
                if (AdminModel != null)
                {
                    return View(AdminModel);
                }
                else
                {
                    return Json(SimCMS.Common.MessageHelper.SendMessge("管理员已经删除，请刷新页面", Common.MessageType.Info, false));
                }
            }
            return View();
        }

        public void GetRoleList()
        {
            //List<SelectListItem>   List = new List<SelectListItem>();
            var Modelist = SimadminroleManager.LoadAll();
            //foreach(var model in Modelist)
            //{
            //    List.Add(new SelectListItem(){Value=model.Id.ToString(),Text=model.RoleName});
            //}
            SelectList VList = new SelectList(Modelist, "Id", "RoleName");
            ViewData["List"] = VList;
        }

        public ActionResult AdminRole()
        {
            var Modelist = SimadminroleManager.LoadAll();
            ViewBag.Total = Modelist.Count;
            return View(Modelist);
        }

        public ActionResult MenuList()
        {
            var Modelist = SimadminmenuManager.LoadAll();
            return View(Modelist);
        }


        [HttpPost]
        public ActionResult Save(Simadmin Model)
        {
            if (Model.Id == 0)
            {
                SimadminManager.Save(Model);
            }
            else
            {
                SimadminManager.Update(Model);
            }
            return Json(SimCMS.Common.MessageHelper.SendMessge("保存成功", Common.MessageType.Info, true, "AdminList"));
        }

        [HttpPost]
        public ActionResult SetRolePermisson()
        {
            var Ids= Request["name"];
            return View();
        }
    }
}
